package com.asa.chainmapview

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.util.Log
import android.view.View
import androidx.core.graphics.PathParser
import org.w3c.dom.NodeList
import java.lang.Exception
import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory
import android.os.Looper

import android.graphics.RectF

import android.annotation.SuppressLint

import android.R
import android.os.Handler
import org.w3c.dom.Document
import org.w3c.dom.Element
import java.io.InputStream


/**
 * create By：anderson
 * on 2022/3/19
 * desc:
 */
class ChinaMapView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private val colors = arrayOf(0xFF239BD7, 0xFF30A9E5, 0xFF80CBF1, 0xFFFFFFFF)
    private val paint = Paint()
    private val provinceItemList = mutableListOf<ProvinceItem>()
    private var selectProvinceItem:ProvinceItem ? = null
    private val totalRect = RectF()

    private val task :Thread = Thread {
//        try {
//            val inputStream: InputStream = context.resources.openRawResource(R.raw.china)
//            val factory = DocumentBuilderFactory.newInstance() //取得DocumentBuilderFactory实例
//
//            var builder: DocumentBuilder? = null //从factory获取DocumentBuilder实例
//
//            try {
//                builder = factory.newDocumentBuilder()
//                val doc: Document = builder.parse(inputStream) //解析输入流 得到Document实例
//                val rootElement: Element = doc.getDocumentElement()
//                val items: NodeList = rootElement.getElementsByTagName("path")
//                var left = -1f
//                var right = -1f
//                var top = -1f
//                var bottom = -1f
//                val list: MutableList<ProvinceItem> = ArrayList()
//                for (i in 0 until items.length) {
//                    val element: Element = items.item(i) as Element
//                    val pathData: String = element.getAttribute("android:pathData")
//                    @SuppressLint("RestrictedApi") val path =
//                        PathParser.createPathFromPathData(pathData)
//                    val proviceItem = ProvinceItem(colors.get(i % 4).toInt(),path)
//                    val rect = RectF()
//                    path.computeBounds(rect, true)
//                    left = if (left == -1f) rect.left else Math.min(left, rect.left)
//                    right = if (right == -1f) rect.right else Math.max(right, rect.right)
//                    top = if (top == -1f) rect.top else Math.min(top, rect.top)
//                    bottom = if (bottom == -1f) rect.bottom else Math.max(bottom, rect.bottom)
//                    list.add(proviceItem)
//                }
//                provinceItemList.clear()
//                provinceItemList.addAll(list)
//                totalRect.set(left, top, right, bottom)
//                //                刷新界面
//                val handler = Handler(Looper.getMainLooper())
//                handler.post(Runnable {
//                    requestLayout()
//                    invalidate()
//                })
//            } catch (e: Exception) {
//                e.printStackTrace()
//            }
//        }catch (e:Exception){
//
//        }
    }
    init {
        task.start()
    }



    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        Log.e("日志", "onDraw: ${provinceItemList.size}}")
        if (provinceItemList.isNotEmpty()){
            provinceItemList.forEach { provinceItem ->
                provinceItem.drawMap(canvas,paint,false)
            }
        }
    }

}